package com.zzsy.shoppingpro.user.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shizhefei.view.indicator.FixedIndicatorView;
import com.shizhefei.view.indicator.IndicatorViewPager;
import com.shizhefei.view.indicator.slidebar.LayoutBar;
import com.shizhefei.view.indicator.slidebar.ScrollBar;
import com.zzsy.shoppingpro.R;
import com.zzsy.shoppingpro.home.acitivty.HomeActivity;

public class WelcomeActivity extends Activity implements View.OnClickListener{


    private TextView entryBtn;
    private TextView tvTiao;
    /**
     * 引导页图片数组
     * */
    private int[] mImgUrlList = new int[]{R.drawable.welcome_page_img_1, R.drawable.welcome_page_img_2, R.drawable.welcome_page_img_3};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);
        initUI();
    }

    /**
     * 初始化数据
     * */

    private void initUI() {
        entryBtn = findViewById(R.id.entry_btn);
        tvTiao = findViewById(R.id.tv_tiao);
        entryBtn.setOnClickListener(this);
        tvTiao.setOnClickListener(this);
        ViewPager viewPager = findViewById(R.id.content_viewpager);
        FixedIndicatorView indicatorview = findViewById(R.id.indicator_view);
        indicatorview.setScrollBar(new LayoutBar(this,R.layout.view_indicator_checked, ScrollBar.Gravity.CENTENT));
        indicatorview.setVisibility(View.GONE);
        IndicatorViewPager indicatorViewPager = new IndicatorViewPager(indicatorview,viewPager);
        indicatorViewPager.setAdapter(new IndicatorViewPager.IndicatorViewPagerAdapter() {
            @Override
            public int getCount() {
                return mImgUrlList.length;
            }

            @Override
            public View getViewForTab(int position, View convertView, ViewGroup container) {
             if(convertView == null){
                 convertView = LayoutInflater.from(WelcomeActivity.this).inflate(R.layout.view_indicator_uncheck,container,false);
             }
                return convertView;
            }

            @Override
            public View getViewForPage(int position, View convertView, ViewGroup container) {
                if(convertView == null){
                    convertView = LayoutInflater.from(WelcomeActivity.this).inflate(R.layout.item_welcome_viewpager,container,false);
                }
                ImageView imageView = convertView.findViewById(R.id.show_image);
                imageView.setScaleType(ImageView.ScaleType.FIT_XY);
                try {
                    Glide.with(WelcomeActivity.this).load(mImgUrlList[position]).into(imageView);
                }catch (Exception e){
                    e.printStackTrace();
                }

                return convertView;
            }
        });
        indicatorViewPager.setOnIndicatorPageChangeListener(new IndicatorViewPager.OnIndicatorPageChangeListener() {
            @Override
            public void onIndicatorPageChange(int preItem, int currentItem) {
                if(currentItem == mImgUrlList.length -1){
                    entryBtn.setVisibility(View.VISIBLE);
                    tvTiao.setVisibility(View.INVISIBLE);
                }else{
                    tvTiao.setVisibility(View.VISIBLE);
                    entryBtn.setVisibility(View.INVISIBLE);
                }
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.tv_tiao:
                startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
                finish();
                break;
            case R.id.entry_btn:
                startActivity(new Intent(WelcomeActivity.this, HomeActivity.class));
                finish();
                break;
        }
    }
}
